package com.weiwei.base.activity.me;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.hwtx.jytxdh.R;
import com.weiwei.base.adapter.VsMyRedAdapter;
import com.weiwei.base.application.VsApplication;
import com.weiwei.base.dataprovider.DfineAction;
import com.weiwei.base.dataprovider.GlobalVariables;
import com.weiwei.base.dataprovider.VsUserConfig;
import com.weiwei.base.util.MyButton;
import com.weiwei.base.util.MyButton.CountDownOnClickListener;
import com.weiwei.json.me.JSONArray;
import com.weiwei.json.me.JSONException;
import com.weiwei.json.me.JSONObject;
import com.weiwei.netphone.data.process.CoreBusiness;

public class VsMyRedPopActivity extends Activity{

	private String uid = "";
	private EditText money_get_edit;
	private MyButton money_btn;
	private ImageView money_img;
	private TextView weixin;
	private Boolean time_set = true;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		requestWindowFeature(Window.FEATURE_NO_TITLE);  
//	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,    
//	    WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		setContentView(R.layout.my_red_popwindow);
		uid = VsUserConfig.getDataString(this, VsUserConfig.JKey_KcId, "");
		init();
		String wx_id = VsUserConfig.getDataString(this, VsUserConfig.JKey_Weixin);
		if (null != wx_id&&wx_id.length()>0) {
			weixin.setText("已绑定");
		}else {
			weixin.setText("未绑定");
		}
		IntentFilter mycalllogFilter = new IntentFilter();
		mycalllogFilter.addAction(VsUserConfig.JKey_GET_MY_REDMONEY);
		getApplicationContext().registerReceiver(mycalllogReceiver, mycalllogFilter);
		
		money_btn.setCountDownOnClickListener(new CountDownOnClickListener() {
			
			@Override
			public void onClickListener() {
				money_btn.startCountDown("提交中","提交");
				getMoney();
			}
		});
		
		
//		money_btn.setOnClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				long nowTime = getTime();
//				long secTime = VsUserConfig.getDataLong(getApplicationContext(), VsUserConfig.JKey_tiem_set);
//				if (nowTime - secTime<60000) {
//					Toast.makeText(getApplicationContext(), "操作频繁，稍后再试", Toast.LENGTH_SHORT).show();
//				}else {
//					getMoney();
//				}
//			
//			}
//		});
		money_get_edit.addTextChangedListener(new TextWatcher()
		 {
		     public void afterTextChanged(Editable edt)
		     {
		         String temp = edt.toString();
		         int posDot = temp.indexOf(".");
		         if (posDot <= 0) return;
		         if (temp.length() - posDot - 1 > 2)
		         {
		             edt.delete(posDot + 3, posDot + 4);
		         }
		     }
		     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
		     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
		 });
		
		money_img.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		}); 
		
		
		
		
		VsApplication.getInstance().addActivity(this);
	}

	private void init() {
		 money_get_edit = (EditText)findViewById(R.id.money_get_edit); 
         money_btn = (MyButton)findViewById(R.id.money_btn); 
         money_img = (ImageView)findViewById(R.id.money_img); 
         weixin = (TextView)findViewById(R.id.weixin);
}

	private void getMoney(){
//		http://139.129.30.128:30000/1.0.0/user/drawing
//		{"uid": "123456", "app_id": "wind", "sign": "7574cbeea5c3fee044e22173f1d3a6b0", "agent_id": "", "remark": "test", "pv": "iphone", "ts": 1453445299, "amount": 1000,
//		"token": "C-US.V36WC6K06J1_IT0Q4XITKK9T-5_J2SKEXXKWS8KRCJCD7B09RTMKUVULMCN3JEQXI36H1JEY58Z2UYBWIPL.G_8-J6TZ0V43UGF4TCHES6BGU24S3JUD3DHC3MX", "v": "1.0.0"}
//		amount：提现金额(单位：分)
//		remark：提现的备注信息(允许为空)
	TreeMap<String, String> treeMap = new TreeMap<String, String>();
	String amount = money_get_edit.getText().toString().trim();
	if (amount != null && amount.length() != 0&&Double.parseDouble(amount)>0) {
		amount = (Double.parseDouble(amount)*100 +"").substring(0, (Double.parseDouble(amount)*100 +"").indexOf("."));
		treeMap.put("uid", uid);
		treeMap.put("amount", amount);
		treeMap.put("remark", "");
		CoreBusiness.getInstance().startThread(getApplicationContext(), GlobalVariables.GetMyRedMoney, DfineAction.authType_AUTO, treeMap, GlobalVariables.actionGetMyRedMoney);	
	}else {
		Toast.makeText(getApplicationContext(), "输入的金额有误", Toast.LENGTH_SHORT).show();
	}
	
	};	
	
	
	
	private BroadcastReceiver mycalllogReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			
			Bundle bundle = intent.getExtras();
		
				String total = bundle.get("list").toString();
				Toast.makeText(getApplicationContext(), total, Toast.LENGTH_SHORT).show();
				if (total.contains("成功")) {
					
					VsUserConfig.setData(getApplicationContext(), VsUserConfig.JKey_tiem_set,getTime());
					finish();
				}
			
			
		}

	};
	
	
	private long getTime(){
		Calendar calendar = Calendar.getInstance();
		String time = DateFormat.format("yyyy-MM-dd kk:mm:ss", calendar.getTime()).toString();
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		
		Date date;
		long time1 = 0;
		try {
			date = format.parse(time);
			 time1 = date.getTime();
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return time1;
	}
	
	
}